const router = require('koa-router')()
const { getToken, Updateurl } = require('../../config/databaseapi.js')

// 更新帖子接口
router.post('/updatetopic', async (ctx) => {
  console.log('调用更新帖子接口')
  // console.log(ctx.request.body);
  const _id = ctx.request.body._id;
  let data = {};
  for(let i in ctx.request.body){
    if(i!=='_id'){
      data[i] = ctx.request.body[i]
    }
  }
  // console.log('_id',_id);
  // console.log('data',data);
  const res = await new getToken().posteve(Updateurl,`db.collection("topic").where({_id:"${_id}"}).update({data:${JSON.stringify(data)}})`);
  if(res.errcode === 0){
    ctx.body={msg:"修改成功",status:1}
  }else{
    ctx.body={msg:"修改失败",status:0}
  }
})

module.exports = router.routes()